package com.tqjc.system.core.threadpool;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.tqjc.system.core.util.ApplicationContextUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

/**
 * @title ThreadPoolManager.java
 * @description 
		  线程池执行类  单例
 * @time  2019年12月4日 下午5:04:26
 * @author 麻木神
 * @version 1.0
*/
public enum ThreadPoolManager {
	INSTANCE;

//	private ThreadPoolManager() {}


	//线程池
	private ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 6, 15L, TimeUnit.SECONDS,
			new LinkedBlockingQueue<>(),
			(ThreadFactory) Thread::new);

//	public static void init(){
//		threadPoolExecutor.execute(null);
//
//	}
	//交给线程池执行
	public  void execute(Runnable runnable) {
//		ThreadPoolExecutor threadPoolExecutor = ApplicationContextUtil.getContext().getBean(ThreadPoolExecutor.class);
		threadPoolExecutor.execute(runnable);
	}
	
	/**
	 * 
	 * @description
	 *  	关闭线程池，不再接受新的任务，之前提交的任务等执行结束再关闭线程池
	 * @return void
	 * @author 麻木神
	 * @time 2019年12月4日 下午5:28:08
	 */
	public void shutdown() {
		if(!threadPoolExecutor.isShutdown()) {
			threadPoolExecutor.shutdown();
		}
	}
}
